<?php
namespace app\index\controller;
use think\View;
use think\Controller;
use think\Db;
use think\Session;
use app\index\model\Message;
use app\index\model\Review;
class Write extends Controller{

  public function index(){
  	$list = Message::all(function($query){
    	$query->order('id', 'desc');
		});
		$view = new View;

		//设置变量输出
		$view->assign('list',$list);
		//return $view->fetch('/write/index');
		
		$re_list = Review::all();

		//$view = new View;

		//设置变量输出
		$view->assign('re_list',$re_list);
		return $view->fetch('/write/index');
  	return $view->fetch('index');

  }
  
  //用户注册
  public function write(){
	 if (!Session::has('username')) {
        return $this->success("请先登录","login/index");
        exit();
      }else{
      	//实例化User
		      $Message = new Message;
		    //接收前端表单提交的数据
		    $Message->title = input('post.message_title');
		    $Message->content = input('post.message_content');
				$Message->user = Session::get('username');
				$Message->face = Session::get('face');
				$Message->lastdate = date('Y-m-d H:i:s',time());
				//进行规则验证
		    $result = $this->validate(
		      [
		        'title' => $Message->title,
		        'content' => $Message->content,
		      ],
		      [
		        'title' => 'require|min:2',
		        'content' => 'require',
		      ]);
			  
		    if (true !== $result) {
		      $this->error($result);
		    }
			
		
		    //写入数据库
		    if ($Message->save()) {
		      return $this->success("留言成功","Write/message");
			  exit();
		    } else {
		      return $this->error('留言失败',"Write/message");
		    }
      }
    
    
    
  }
  
  public function review(){
  	 //实例化User
		      $Review = new Review;
		    //接收前端表单提交的数据
		    
		    $Review->re_content = input('post.re_content');
				$Review->by_reviewer_messageid = input('post.id');
				$Review->by_reviewer = input('post.user');
				$Review->reviewer = Session::get('username');
				$Review->face = Session::get('face');
				$Review->re_datetime = date('Y-m-d H:i:s',time());
				//进行规则验证
		    $result = $this->validate(
		      [
		        'content' => $Review->re_content,
		      ],
		      [
		        'content' => 'require',
		      ]);
			  
		    if (true !== $result) {
		      $this->error($result);
		    }
			
				
		    //写入数据库
		    if ($Review->save()) {
		      return $this->success('留言成功');
			  exit();
		    } else {
		      return $this->error('留言失败');
		    }
				
		    $re_list = Review::all();
		
				$view = new View;
		
				//设置变量输出
				$view->assign('re_list',$re_list);
				return $view->fetch('/write/index');
      
    
    
  }
  
	public function Message(){
		//echo $id;
		$list = Message::all(function($query){
    	$query->order('id', 'desc');
		});

		$view = new View;

		//设置变量输出
		$view->assign('list',$list);
		
		$re_list = Review::all();
	
		//设置变量输出
		$view->assign('re_list',$re_list);
		return $view->fetch('/write/index');
	}
	
	public function logout(){
		Session::delete('username');
		Session::delete('face');
		$view = new View;
		return $view->fetch('/index/index');
	}
	
}